Skip to content

feat(adapters): any Gymnasium environment as a game - #10

Merged
drewstone merged 1 commit into
mainfrom
feat/gymnasium-adapter
Aug 20, 2026
Merged

drewstone merged 1 commit into
mainfrom
feat/gymnasium-adapter

Conversation

@drewstone

Copy link
Copy Markdown
Member

Purpose

Any registered Gymnasium environment with a Discrete action space becomes a Playproof Game. One worker covers classic control, toy text, procedurally generated suites, and text environments, including third-party environments that register with Gymnasium. MultiDiscrete, MultiBinary, and Box action spaces are refused at boot with a readable message; a word-to-vector encoding for a continuous action is a design decision this adapter does not make for the caller.

The limit is stated instead of hidden. A generic environment has no privileged channel the agent cannot author: reward, info, and the observation are exactly what the environment hands the policy. Read the tier as reward-derived, not hidden. Verification still holds because it is replay — the verifier re-executes from the seed and the input log and recomputes every milestone.

What changes

  • gym/worker.py — Gymnasium over the shared line-JSON worker protocol: boot, reset, step, evidence, frame, inputs, snapshot, checkpoint, restore, shutdown.
  • adapters/gym-rpc.ts, adapters/gymnasium.ts — WorkerRpc specialization and makeGymnasium({ envId, seed?, kwargs?, python?, reference? }).
  • gym/reference-cartpole.json, gym/reference-frozenlake.json — committed reference playthroughs. deriveContract replays them and samples every milestone value and hash; no threshold or hash is written by hand.
  • gymnasium.test.mts and pnpm test:gym — the adapter gate, skipped with one line when Gymnasium is absent and loud under PLAYPROOF_REQUIRE_GYM=1.
  • CI job real-gymnasium, tsup entries, the ./adapters/gymnasium and ./adapters/gym-rpc exports, asset copying, the packed-tarball requirement, the boundary prefix, README, docs/adapters.md, and a 0.3.0 changelog section.

Design points worth review:

  • Inputs. NOOP plus one word per action, named from get_action_meanings() when the environment exposes it and a0 … a{n-1} otherwise. A discrete space has no guaranteed idle action, so NOOP and every unknown word do not call env.step at all — the state is unchanged. An agent typo is not a cheat, and it must not silently advance the episode either. An environment that names its own action NOOP, as ALE does, keeps that real action.
  • Evidence. cumulativeReward, steps, terminated, truncated, and the numeric info entries bounded to the first 16, plus frameHash over the whole observation and frameState over its first 8 components. Playproof evidence is integer-only, so reward, numeric info entries, and observation components are multiplied by 1000 and rounded: reward 1.0 is cumulativeReward 1000.
  • Checkpoints. Gymnasium exposes no generic state API, so the general checkpoint is {seed, inputs} and restore is reset(seed) plus a replay. Where the environment keeps its position in one readable attribute (state for classic control, s for toy text), the worker also writes a JSON copy of it and restores it directly. pickle is never used, so a checkpoint stays a plain protocol value a verifier can read.

Verification

pnpm run ci in a clean worktree:

playproof-boundary: 88 files checked; framework is research-harness- and provider-neutral
playproof: 22 calibration gates green + review-fix regressions green
playproof episode: loop, budget, and partial-progress semantics OK
playproof-platform: native process, frontier replay, explicit trust modes, signed execution, and red-team gates green
playproof-desktop-platforms: hardened native process, Steam, Xbox, bounded IO, and signed recorder composition green
playproof-drivers: CLI and OpenAI-compatible adapters green
playproof campaign: segment, steer, resume, and tamper semantics OK
packed Playproof imports verified
playproof-package: 79 packed entries verified

The adapter gate on gymnasium 1.3.0 / numpy 2.5.2 / CPython 3.12.10:

$ PLAYPROOF_REQUIRE_GYM=1 PLAYPROOF_PYTHON=…/gym-venv/bin/python pnpm test:gym
gymnasium: CartPole-v1 on Discrete(2) and FrozenLake-v1 — derivation, 5+3-milestone
contracts, known-good, graded partial, false-claim, cross-process determinism over 121
snapshots, engine and replay checkpoints, unknown-input no-op, teardown OK (reference
balances 120 steps for reward 120)

What that gate covers:

  • Determinism, measured not assumed. The 120-input CartPole reference replays twice in one worker and once in a freshly spawned worker; all 121 evidence snapshots and frame hashes are identical across processes. The same holds for the FrozenLake reference. Cross-process is the load-bearing case, because a verifier never shares the environment that produced the run.
  • Known-good and false-claim. The reference verifies all 5 CartPole and all 3 FrozenLake milestones. A garbage script of the same length claiming the same milestones verifies none of them and is rejected. On FrozenLake, walking into a hole earns nothing.
  • Graded, not all-or-nothing. A 30-input partial replay earns survived-25-steps, reward-at-25-steps, and frame-at-25-steps, and is rejected only for the two it claims beyond.
  • Both checkpoint paths round-trip at input 40 and reproduce the same evidence for the next 8 inputs; a garbage payload is rejected.
  • Unknown inputs and a terminated episode both leave the evidence unchanged.
  • Teardown. dispose() kills the worker and every later call fails loudly.

Also checked: python -m compileall -q gym passes, the skip line prints when Gymnasium is missing, and PLAYPROOF_REQUIRE_GYM=1 turns that miss into a failure.

Add one out-of-process worker that turns any registered Gymnasium
environment with a Discrete action space into a Playproof game: classic
control, toy text, procedurally generated suites, and text environments.

Evidence is the environment's own cumulative reward, step count,
termination flags, and numeric info entries, joined by a hash of the whole
observation and a bounded projection of it. Reward and numeric info
entries are scaled by 1000, because Playproof evidence is integer-only.

The limit is stated, not hidden: a generic environment offers no
privileged channel the agent cannot author, so the tier is reward-derived
and verification rests on replay recomputation alone.

Gymnasium exposes no generic state API. A checkpoint therefore replays
from its seed, and also writes back the environment's own state attribute
where one is readable. No pickle is involved.

Contracts come from committed reference playthroughs on CartPole-v1 and
FrozenLake-v1. Both environments ship inside Gymnasium, so the new CI job
runs on a clean machine with no asset.
@drewstone
drewstone merged commit 23d478a into main Aug 20, 2026
4 checks passed
@drewstone
drewstone deleted the feat/gymnasium-adapter branch August 20, 2026 23:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant